package org.jiuling.appletofwechat;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.jiuling.appletofwechat.enums.AppletOfWeChatApiEnum;
import org.jiuling.appletofwechat.model.PhoneInfoModel;
import org.jiuling.local.tools.enums.IDDAreaCodeEnum;
import org.jiuling.local.tools.exception.LocalToolsException;
import org.jiuling.local.tools.util.EnumUtil;
import org.jiuling.local.tools.verify.LongVerify;
import org.jiuling.local.tools.verify.StringVerify;
import org.jiuling.network.tools.http.request.HttpRequest;
import org.jiuling.network.tools.http.request.exception.RequestException;
import org.jiuling.network.tools.http.request.factory.HttpRequestFactory;
import org.jiuling.network.tools.http.request.model.HttpRequestBody;
import org.jiuling.network.tools.http.request.model.HttpRequestParam;
import org.jiuling.network.tools.response.ResponseData;
import org.jiuling.wechat.base.WeChatAuthorization;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

/**
 * 微信小程序用户信息
 * @author 紫灵
 * @date 2024年01月29日 12时33分37秒
 */
public class AppletOfWeChatUserInfo {

    private final WeChatAuthorization weChatAuthorization;

    public AppletOfWeChatUserInfo(WeChatAuthorization weChatAuthorization) {
        this.weChatAuthorization = weChatAuthorization;
    }

    /**
     * 获取用户手机号
     * @param code 验证码
     * @return 电话信息模型
     */
    public ResponseData<PhoneInfoModel> getUserPhoneNumber(String code){
        AppletOfWeChatApiEnum weChatMiniprogramApi = AppletOfWeChatApiEnum.GET_USER_PHONE_NUMBER;
        // 封装参数
        HttpRequestParam httpRequestParam = new HttpRequestParam()
                .add("access_token",weChatAuthorization.getToken().getAccessToken());
        try {
            HttpRequestBody httpRequestBody = new HttpRequestBody()
                    .add("code",code);
            HttpRequest httpRequest = new HttpRequest();
            HttpRequestFactory httpRequestFactory = httpRequest.openHttpRequestFactory(weChatMiniprogramApi.getApi());
            httpRequestFactory.setRequestParam(httpRequestParam);
            httpRequestFactory.setRequestBody(httpRequestBody);
            httpRequestFactory.setRequestMethod(weChatMiniprogramApi.getRequestMethod());
            JSONObject jsonObject = JSON.parseObject(httpRequest.send().getBody());
            if(!LongVerify.isBlank(jsonObject.getLong("errcode"))){
                return new ResponseData<>(jsonObject.getInteger("errcode"),jsonObject.getString("errmsg"),null);
            }
            JSONObject phoneInfoJson = jsonObject.getJSONObject("phone_info");
            JSONObject watermarkJson = phoneInfoJson.getJSONObject("watermark");
            PhoneInfoModel.WatermarkModel watermarkModel = PhoneInfoModel.WatermarkModel.builder()
                    .addAppId(watermarkJson.getString("appid"))
                    .addTimeStamp(watermarkJson.getInteger("timestamp"))
                    .build();
            IDDAreaCodeEnum iddAreaCode = null;
            String countryCode = phoneInfoJson.getString("countryCode");
            if(!StringVerify.isBlank(countryCode)){
                iddAreaCode = EnumUtil.valueOfEnum(IDDAreaCodeEnum.class,countryCode,"getAreaCode");
            }
            PhoneInfoModel phoneInfoModel = PhoneInfoModel.builder()
                    .addPhoneNumber(phoneInfoJson.getString("phoneNumber"))
                    .addPurePhoneNumber(phoneInfoJson.getString("purePhoneNumber"))
                    .addIddAreaCode(iddAreaCode)
                    .addWatermark(watermarkModel)
                    .build();
            return new ResponseData<>(jsonObject.getInteger("errcode"),jsonObject.getString("errmsg"),phoneInfoModel);
        } catch (RequestException | IOException | LocalToolsException | NoSuchAlgorithmException |
                 KeyManagementException e) {
            throw new RuntimeException(e);
        }
    }

}
